<?php

class Question extends Controller {

    public function __construct() {
        parent::Controller();
        $this->load->model('questionmodel', 'question');
        $this->load->model('anwsermodel', 'anwser');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 10;
        $total = $this->question->q_count();
        $offset = intval($this->input->get('id'));

        $config['base_url'] = base_url() . 'index.php/question/index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '在线咨询';
        $data['questions'] = $this->question->q_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();

        $data['advisers'] = $this->db->get('adviser')->result();
        $this->load->view('question', $data);
    }

    public function add() {
        if (isset($_POST['submit'])) {
            $data['author'] = htmlspecialchars($this->input->post('author'));
            $data['title'] = htmlspecialchars($this->input->post('title'));
            $data['content'] = htmlspecialchars($this->input->post('content'));
            $data['created_at'] = date('Y-m-d h:i:s', time());
            $label = array('author' => '称呼', 'title' => '标题', 'content' => '内容');
            foreach ($data as $key => $da)
                if (empty($da))
                    die (json_encode(array('statu'=>0,'detail'=>$label[$key] . '不能为空！')));
            if ($this->db->insert('question', $data))
                die (json_encode(array('statu'=>1,'detail'=>'提交成功！')));
        }else{
            die (json_encode(array('statu'=>0,'detail'=>'提交失败，请联系管理员！')));
        }
    }

}
